﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DocumentManagement.ViewsModels;
using DocumentManagement.Models;
using System.Data;

namespace DocumentManagement.Views
{
    /// <summary>
    /// Interaction logic for CreateDocumentINN.xaml
    /// </summary>
    public partial class CreateDocumentINN : Window
    {
        CreateDocumentINNViewsModel _createDocumentINNViewsModel;
        public CreateDocumentINN(CreateSend createSend, QSHCEntities1 entity)
        {
            InitializeComponent();
            _createDocumentINNViewsModel = this.Resources["createDocumentINNViewsModel"] as CreateDocumentINNViewsModel;
            _createDocumentINNViewsModel.EntityModels = entity;
            _createDocumentINNViewsModel.MyCreateSend = createSend;


            switch (createSend.Speed)
            {
                case 1:
                    _createDocumentINNViewsModel.MySpeed1 = true;
                    break;
                case 2:
                    _createDocumentINNViewsModel.MySpeed2 = true;
                    break;
                case 3:
                    _createDocumentINNViewsModel.MySpeed3 = true;
                    break;
                case 4:
                    _createDocumentINNViewsModel.MySpeed4 = true;
                    break;
                default:
                    break;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            

            FromDivisionComboBox.SelectedValue = 17;
            FromDivisionComboBox.IsEnabled = false;

            DateTextBlock.Visibility = Visibility.Hidden;
            CreateSendCodeTextBox.Background = Brushes.AliceBlue;
            NameInstitutionTextBox.Background = Brushes.AliceBlue;
            TitleTextBox.Background = Brushes.AliceBlue;
            ToPositionTextBox.Background = Brushes.AliceBlue;
            Paragraph1TextBox.Background = Brushes.AliceBlue;
            PostScriptTextBox.Background = Brushes.AliceBlue;
            EndorserTextBox.Background = Brushes.AliceBlue;
            PositionTextBox.Background = Brushes.AliceBlue;

            if (_createDocumentINNViewsModel.MyCreateSend.EntityState == EntityState.Added || _createDocumentINNViewsModel.MyCreateSend.EntityState == EntityState.Detached)
            {
                _createDocumentINNViewsModel.GetAutoNumber();
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (CreateSendCodeTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกเลขที่หนังสือ", "กรอกข้อมูลไม่ครบ");
                CreateSendCodeTextBox.Focus();
                return;
            }

            if (NameInstitutionTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกชื่อหน่วยงานราชการ", "กรอกข้อมูลไม่ครบ");
                NameInstitutionTextBox.Focus();
                return;
            }

            if (TitleTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกชื่อเรื่องของหนังสือ", "กรอกข้อมูลไม่ครบ");
                TitleTextBox.Focus();
                return;
            }
            if (ToPositionTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกเรียนหนังสือถึง..", "กรอกข้อมูลไม่ครบ");
                ToPositionTextBox.Focus();
                return;
            }

            if (Paragraph1TextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกย่อหน้าแรก", "กรอกข้อมูลไม่ครบ");
                Paragraph1TextBox.Focus();
                return;
            }
          
            if (PostScriptTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกคำลงท้ายหนังสือ", "กรอกข้อมูลไม่ครบ");
                PostScriptTextBox.Focus();
                return;
            }
            if (EndorserTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกผุ้ลงนามในหนังสือ", "กรอกข้อมูลไม่ครบ");
                EndorserTextBox.Focus();
                return;
            }

            if (PositionTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกตำแหน่งงาน", "กรอกข้อมูลไม่ครบ");
                PositionTextBox.Focus();
                return;
            }



            _createDocumentINNViewsModel.SaveToData();
            PageSwitcher.Switch(new ShowItemCreateDocumentUserControl());

        }

        private void PrintPreviewButton_Click(object sender, RoutedEventArgs e)
        {
            if (CreateSendCodeTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกเลขที่หนังสือ", "กรอกข้อมูลไม่ครบ");
                CreateSendCodeTextBox.Focus();
                return;
            }

            if (NameInstitutionTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกชื่อหน่วยงานราชการ", "กรอกข้อมูลไม่ครบ");
                NameInstitutionTextBox.Focus();
                return;
            }

            if (TitleTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกชื่อเรื่องของหนังสือ", "กรอกข้อมูลไม่ครบ");
                TitleTextBox.Focus();
                return;
            }
            if (ToPositionTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกเรียนหนังสือถึง..", "กรอกข้อมูลไม่ครบ");
                ToPositionTextBox.Focus();
                return;
            }

            if (Paragraph1TextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกย่อหน้าแรก", "กรอกข้อมูลไม่ครบ");
                Paragraph1TextBox.Focus();
                return;
            }
        
            if (PostScriptTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกคำลงท้ายหนังสือ", "กรอกข้อมูลไม่ครบ");
                PostScriptTextBox.Focus();
                return;
            }
            if (EndorserTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกผุ้ลงนามในหนังสือ", "กรอกข้อมูลไม่ครบ");
                EndorserTextBox.Focus();
                return;
            }

            if (PositionTextBox.Text == "")
            {
                MessageBox.Show("กรุณากรอกตำแหน่งงาน", "กรอกข้อมูลไม่ครบ");
                PositionTextBox.Focus();
                return;
            }

            PrintPreviewDocumentIn printCreateDoc = new PrintPreviewDocumentIn(_createDocumentINNViewsModel.MyCreateSend, _createDocumentINNViewsModel.EntityModels);
            printCreateDoc.ShowDialog();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void PrintButton_Click(object sender, RoutedEventArgs e)
        {
            stackPanel55.Visibility = Visibility.Collapsed;
            CreateSendDatePicker.Visibility = Visibility.Collapsed;
            DateTextBlock.Visibility = Visibility.Visible;
            stackPanel11.Visibility = Visibility.Collapsed;
            PostScriptTextBlock.Visibility = Visibility.Hidden;
            EndorserTextBlock.Visibility = Visibility.Hidden;
            PositionTextBlock.Visibility = Visibility.Hidden;
            stackPanel17.Visibility = Visibility.Collapsed;

            CreateSendCodeTextBox.IsEnabled = true;
            CreateSendCodeTextBox.Background = Brushes.White;
            NameInstitutionTextBox.Background = Brushes.White;
            TitleTextBox.Background = Brushes.White;
            ToPositionTextBox.Background = Brushes.White;
            Paragraph1TextBox.Background = Brushes.White;
            PostScriptTextBox.Background = Brushes.White;
            EndorserTextBox.Background = Brushes.White;
            PositionTextBox.Background = Brushes.White;


            PrintDialog dialog = new PrintDialog();

            if (dialog.ShowDialog() != true) return;

            grid2.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));
            //grid2.Arrange(new Rect(new Point(50, 50), grid2.DesiredSize));

            dialog.PrintVisual(grid2, "A WPF printing");
        }
    }
}
